// pages/mine/allorder/orderdetail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
  imgurl:app.globalData.imgurl,
  options: [
    {
      status: 0,
      btns: [
        {
          text: '马上付款',
          event: 'pay'
        // }, {
        //   text: '取消',
        //   event: 'cancel'
        }
      ]
    }, {
      status: 1,
      btns: []
    }, {
      status: 2,
      btns: []
    }, {
      status: 3,
      btns: []
    }
  ]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that= this;
  wx.request({
    url: app.globalData.url +'order_xq?order_id='+e.id,
    success:res =>{
      console.log(res)
        var detail = res.data[0];
        detail.priceset = that.getprice(detail)
        detail.btns = that.getbtns(detail.type,e)
        that.setData({
          detail: detail,
          orderid: e.id
        })
      }
  })
  },
  backhome:function(e){
    var o = e.currentTarget.dataset
    wx.redirectTo({
      url: '/pages/takeaway/takdetail/takedetail?shopid='+o.shopid,
    })
  },
  getbtns: function (status,e) {
    var options = this.data.options
    var btns
    switch (status) {
      case '0':
        if (!e.chuzu||e.chuzu==''){
          btns = options[0].btns
        }
        break;
      case '1':
        btns = options[1].btns
        break;
      case '2':
        btns = options[2].btns
        break;
      case '3':
        btns = options[3].btns
        break;
    }
    return btns
  },
  // 计算总价
  getprice(e) {
    if(e.type == 0){
      var zujin = e.unit * e.number * e.zi[0].cuxiao_price
      var yajin = e.number * e.zi[0].yajin
      var payPrice = yajin + zujin
      return {
        payPrice: payPrice,
        zujin: zujin,
        yajin: yajin
        
      }
    }else{
      var payPrice = e.number * e.zi[0].cuxiao_price
      return {
        payPrice: payPrice,
        totalPrice: null
      }
    }
    
  },
  // 付款
  pay: function (e) {
    var that = this
    var o = e.currentTarget.dataset
    wx.request({
      url: app.globalData.payurl + 'openid=' + wx.getStorageSync('openId') + '&order_id=' + o.id,
      success: function (res) {
        var payData = res.data
        if (payData.timeStamp) {
          wx.requestPayment({
            timeStamp: payData.timeStamp,
            nonceStr: payData.nonceStr,
            package: payData.package,
            signType: payData.signType,
            paySign: payData.paySign,
            success: function (res) {
              console.log(res)
            },
            fail: function (res) {
              console.log('支付失败')
            },
            complete: function (res) {

            },
          })
        }
      },
      fail: res => {
        console.log('错误')
      }
    })
  },

  //关闭
  onClose: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var openId = app.globalData.openId;
    var orderState = that.data.orderState;
    wx.showModal({
      title: '提示',
      content: '是否关闭此订单',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.url + 'deleteOrder?openId=' + openId + '&orderId=' + id,
            success: function (res) {
              if (res.data == 'ajaxSuccess') {
                wx.showToast({
                  title: '已关闭',
                })
                page = 1;
                that.getOrderList(orderState)
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },


  //物流
  onWL: function (e) {

  },

  //评价
  onEvaluate: function (e) {
    var o = e.currentTarget.dataset;
    console.log(o)
    wx.redirectTo({
      url: '/pages/mine/allorder/evaluate/comment?types=' + o.shoptype + '&id=' + o.id + '&shopid=' + o.shopid,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})